/**
 * @author Eric Jahn 
 * Course: Object Oriented Programming 
 * Final Project
 * Due Date: August 3, 2012
 */
package oop.chat.main;

import oop.chat.connection.Client;
import oop.chat.connection.Server;
import oop.chat.data.ConnectionStatus;
import oop.chat.gui.Gui;
import oop.chat.user.*;

/**
 * main class to instantiate a new client thread
 * @author Spencer Breeden
 */
public class OOPChat
{
    /**
	 * Usage:
     * To start the Client in console mode use the following command: 
     * java Client [-u username password] [-p portNumber] [-s serverAddress] [-t userType] [-c PID firstName lastName [year program]]
     */    
    
    /**
	 * main function
     * @param	args		the command line arguments
     */
    public static void main(String[] args)
    {
        System.out.println("oopChat main");
		ContactList[] contactLists = new ContactList[Category.values().length];
		int index;
		for(oop.chat.user.Category category: oop.chat.user.Category.values())
		{
			index = category.getIndex();
			if(index == oop.chat.user.Category.COMMON_LIST.getIndex())
			{
				contactLists[index] = new ContactList();
			}
			else
			{
				contactLists[index] = new SpecialList(category);
			}
		}
        int portNumber = Server.PORT;
        String serverAddress = "localhost";
		
		//Client 1
		/*Contact contactInfoOne = new Contact("E3354827", "Eric", "Jahn", "2013", "CS");	
		Contact c2 = new Contact("E3354828", "Spencer", "Breeden", "2012", "Computer Science");
        contactLists[Category.COMMON_LIST.getIndex()].addContact(c2);
		
		UserAccount userOne = new UserAccount(UserType.STUDENT, contactInfoOne, contactLists);
		
		//start client 1
		Client clientOne = new Client(serverAddress, portNumber, userOne);
		Gui guiOne = null;
        // start the connection to the Server
        // if it failed nothing we can do
        if (clientOne.start() == ConnectionStatus.CONNECTED)
        {
			guiOne = new Gui(clientOne);
			clientOne.setGui(guiOne);
        }*/
		
		//Client 2
		Contact contactInfoTwo = new Contact("E3354828", "Spencer", "Breeden", "2013", "CS");		
		Contact c1 = new Contact("E3354827", "Eric", "Jahn", "2012", "Computer Science");
        contactLists[Category.COMMON_LIST.getIndex()].addContact(c1);	
		
		UserAccount userTwo = new UserAccount(UserType.STUDENT,  contactInfoTwo, contactLists);
		
		//start client 2
		Client clientTwo = new Client(serverAddress, portNumber, userTwo);
		Gui guiTwo = null;
        // start the connection to the Server
        // if it failed nothing we can do
        if (clientTwo.start() == ConnectionStatus.CONNECTED)
        {
			guiTwo = new Gui(clientTwo);
			clientTwo.setGui(guiTwo);
        }
		
/*
        // default values        
        int portNumber = Server.PORT;
        String serverAddress = "localhost";
        String userName = "anonymous";
        String password = "anonymous";
        UserType type = UserType.STUDENT;
        Contact contactInfo = null;

        //debug
//        for (int i = 0; i < args.length; i++)
//        {
//            System.out.println("args[" + i + "] = " + args[i]);
//        }

        for (int i = 0; i < args.length; i++)
        {
            try
            {
                switch (args[i])
                {
                    case "-u":
                        userName = args[++i];
                        password = args[++i];
                        break;
                    case "-p":
                        portNumber = Integer.parseInt(args[++i]);
                        break;
                    case "-s":
                        serverAddress = args[++i];
                        break;
                    case "-t":
                        type = UserType.valueOf(args[++i]);
                        break;
                    case "-c":
                        String PID = args[++i];
                        String firstName = args[++i];
                        String lastName = args[++i];
                        String year = null;
                        String program = null;
                        if (++i < args.length && args[i].charAt(0) != '-')
                        {
                            year = args[i];
                            program = args[++i];
                        }
                        contactInfo = new Contact(PID, firstName, lastName, year, program);
                        break;
                    case "-h":
                        System.out.println(
                                "Usage: "
                                + "> java Client [-u username password] "
                                + "[-p portNumber] [-s serverAddress] [-t userType] "
                                + "[-c PID firstName lastName [year program]]");
                        break;
                }
            }
            catch (ArrayIndexOutOfBoundsException ex)
            {
                System.out.println(
                        "Usage: "
                        + "> java Client [-u username password] [-p portNumber] "
                        + "[-s serverAddress] [-t userType] "
                        + "[-c PID firstName lastName [year program]]");
                break;
            }
        }
        if (contactInfo == null)
        {  
            // default contact info
            contactInfo = new Contact(userName, "none", "none", "0", "none");
        }
        UserAccount user = new UserAccount(type, password, contactInfo);
        // create the Client object
        Client client = new Client(serverAddress, portNumber, user);
        // start the connection to the Server, if it fails nothing we can do
        if (client.start() != ConnectionStatus.CONNECTED)
        {
            System.out.println("Error starting client");
            return;
        }

        // wait for messages from user
        Scanner scan = new Scanner(System.in);
        // loop forever for message from the user
        boolean logout = false;
        while (!logout)
        {
            System.out.print("> ");
            // read message from user
            String message = scan.nextLine();

            // logout if message is LOGOUT
            if (message.equalsIgnoreCase("LOGOUT"))
            {
                logout = true;
            }
            client.processMessage(message);
        }
		*/
    }
}
